﻿/*
 * Created by SharpDevelop.
 * User: Hellen(LongFan)
 * Date: 2015/12/20
 * Time: 21:07
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Drawing;
using System.Windows.Forms;

namespace Comm
{
	/// <summary>
	/// Description of Form1.
	/// </summary>
	public delegate void MyConfirmParaEventHandler(object sender, EventArgs e);
	public delegate void ConfirmParaResetFreqEventHandler();
		
	public partial class ConfigPara : Form
	{
		public event MyConfirmParaEventHandler ConfirmParaEvent;
		
		public ConfigPara()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
			RadioButton8Bit.Checked = DealDataThread.ByteFormat;
			StartFreqTextBox.Text = MainForm.DriveStartFreq.ToString();
			DeltaFreqTextBox.Text = MainForm.DriveDeltaFreq.ToString();
			DriveFreqLabel.Text = Convert.ToString( MainForm.DriveStartFreq + MainForm.DriveDeltaFreq );
		}
		
		void ConfirmParaButtonClick(object sender, EventArgs e)
		{
			int tempFreq = Convert.ToInt32(DeltaFreqTextBox.Text);
			//check the Delta frequence
			if( 120000 < tempFreq || tempFreq < -120000 )
			{
				MessageBox.Show("频率增量超出范围！\n频率增量范围：-120KHz ~ 120KHz");
				DeltaFreqTextBox.Text = MainForm.DriveDeltaFreq.ToString();
				return ;
			}
			//check the start frequence
			int tempStartFreq = Convert.ToInt32(StartFreqTextBox.Text);
			if( 500 < tempStartFreq || tempStartFreq < 5 )
			{
				MessageBox.Show("初始频率超出范围！\n初始频率增量范围：5Hz ~ 500Hz");
				StartFreqTextBox.Text = "10";
				return ;
			}			
			//check the received data with
			if(RadioButton8Bit.Checked)
			{
				DealDataThread.ByteFormat = true;
			}
			if(RadioButton16Bit.Checked)
			{
				DealDataThread.ByteFormat = false;
			}
			//set the driver frequence
			MainForm.DriveStartFreq = Convert.ToUInt16(StartFreqTextBox.Text);
			MainForm.DriveDeltaFreq = Convert.ToInt32(DeltaFreqTextBox.Text);
			DriveFreqLabel.Text = (Convert.ToUInt16(StartFreqTextBox.Text) + Convert.ToInt32(DeltaFreqTextBox.Text)).ToString() ;
			//send event to mainform	
			ConfirmParaEvent(this, e);
			this.Close();
		}
		
		void AdvLabelDoubleClick(object sender, EventArgs e)
		{
			RadioButton8Bit.Enabled = true ;
		}
		
		void ConfigParaLoad(object sender, EventArgs e)
		{//set the tooltips
			if(!RadioButton8Bit.Enabled)
			{
				toolTip1.SetToolTip(RadioButton8Bit, "双击\"Advance\"解锁高级功能");
			}
			toolTip1.SetToolTip(AdvLabel, "双击\"Advance\"解锁高级功能");
			toolTip1.SetToolTip(ResetParaButton, "复位频率增量和起始频率");
		}
		
		void ResetParaButtonClick(object sender, EventArgs e)
		{
			ConfirmParaResetFreqEventHandler MyResetFreqDelegate = new ConfirmParaResetFreqEventHandler(MainForm.ResetDriverFreq);
			DeltaFreqTextBox.Text = "0";
			StartFreqTextBox.Text = "10";	
			MyResetFreqDelegate();
		}
		
		void StartFreqTextBoxTextChanged(object sender, EventArgs e)
		{
			DriveFreqLabel.Text = (Convert.ToUInt16(StartFreqTextBox.Text) + Convert.ToInt32(DeltaFreqTextBox.Text)).ToString() ;
		}
		
		void DeltaFreqTextBoxTextChanged(object sender, EventArgs e)
		{
			DriveFreqLabel.Text = (Convert.ToUInt16(StartFreqTextBox.Text) + Convert.ToInt32(DeltaFreqTextBox.Text)).ToString() ;	
		}
	}
}
